package com.pri.chat.activity;

import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.MeGiftBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.adapter.GiftMeAdapter;
import com.pri.chat.base.BaseActivity;
import com.pri.chat.model.UserModel;
import com.pri.chat.utils.Des3Util;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.view.RxToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：添加类的描述
 *
 * @author
 * @time 2020/4/30
 */
public class MyGiftActivity extends BaseActivity {
    @BindView(R.id.rec_main)
    RecyclerView recMain;
    @BindView(R.id.none)
    ImageView none;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_gift_layout;
    }

    @Override
    protected void initView() {
        setTitle("我收到的礼物");
        initRv();
    }

    private List<MeGiftBean> mlist;
    private GiftMeAdapter mAdapter;

    private void initRv() {
        mlist = new ArrayList<>();
        GridLayoutManager linearLayoutManager = new GridLayoutManager(this, 3);
        recMain.setLayoutManager(linearLayoutManager);
        recMain.setNestedScrollingEnabled(false);
        mAdapter = new GiftMeAdapter(R.layout.item_gift_me, mlist);
        recMain.setAdapter(mAdapter);
    }
    private int page = 1;
    private boolean isShowDialog = false;
    private void initSrl() {
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh_layout.finishRefresh(1000);
            refresh();
        });
        srlAll.setOnLoadmoreListener(refresh_layout -> {
            refresh_layout.finishLoadmore(1000);
            isShowDialog = false;
            page++;
            initData();
        });
    }

    private void refresh() {
        page = 1;
        isShowDialog = false;
        mlist.clear();
        mAdapter.notifyDataSetChanged();
        initData();
    }
    @Override
    protected void initData() {
        SubscriberOnNextListener<BaseBean<List<MeGiftBean>>> onNextListener = baseBean -> {
            if (baseBean.getData().isEmpty() && page == 1) {
                none.setVisibility(View.VISIBLE);
                mAdapter.notifyDataSetChanged();
                RxToast.normal(getString(R.string.no_data));
            } else {
                none.setVisibility(View.INVISIBLE);
                    mlist.addAll(baseBean.getData());

                mAdapter.notifyDataSetChanged();
            }
        };
        UserModel dtModel = new UserModel();//登录上传参数
        dtModel.setMemberId(SharedHelper.readId(this));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(dtModel));
        HttpMethods.getInstance().receiverLiWu(new ProgressSubscriber<>(onNextListener, this, isShowDialog), requestBody);

    }


}
